package com.canbot.u05.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;

import com.canbot.u05.bean.AttendaceExcelBean;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by X230 on 2017/6/8.
 */

public class ChartTableView extends View {

        private int width;//控件宽

        private int height;//控件高

        private int dataNum;//数据量

        private Paint mPaintText;//用于绘制文本

        private Paint mPaintWhiteBg;//用于绘制白色背景

        private Paint mPaintGreyBg;//用于绘制灰色背景

        private Paint mPaintLightGrey;//用于绘制浅蓝色背景

        private Paint mPaintLine;//用于画表格的列线

        private int textSize = dip2px(getContext(), 13);//文本尺寸，dp转px

        private List<AttendaceExcelBean> dataList;//总数据

        public ChartTableView(Context context) {
                super(context);
        }

        public ChartTableView(Context context, AttributeSet attrs) {
                super(context, attrs);

                mPaintLine = new Paint();
                mPaintLine.setColor(Color.LTGRAY);
                mPaintLine.setStrokeWidth(dip2px(getContext(), 1));
                mPaintLine.setAntiAlias(true);

                mPaintText = new Paint();
                mPaintText.setColor(Color.BLACK);
                mPaintText.setTextSize(textSize);
                mPaintText.setTextAlign(Paint.Align.CENTER);
                mPaintText.setAntiAlias(true);

                mPaintWhiteBg = new Paint();
                mPaintWhiteBg.setColor(Color.WHITE);
                mPaintWhiteBg.setStyle(Paint.Style.FILL);
                mPaintWhiteBg.setAntiAlias(true);


                mPaintGreyBg = new Paint();
                mPaintGreyBg.setColor(Color.argb(255, 240, 240, 240));
                mPaintGreyBg.setColor(Color.parseColor("#818297"));
                mPaintGreyBg.setStyle(Paint.Style.FILL);
                mPaintGreyBg.setAntiAlias(true);

                mPaintLightGrey = new Paint();
                mPaintLightGrey.setColor(Color.argb(255, 250, 250, 250));
                mPaintLightGrey.setStyle(Paint.Style.FILL);
                mPaintLightGrey.setAntiAlias(true);

                dataList = new ArrayList<>();

        }

        /**
         * 得到dataList并确定一部分变量的值
         *
         * @param dataList
         */
        public void setDataList(List<AttendaceExcelBean> dataList) {
                if (dataList != null){
                        this.dataList = dataList;
                        dataNum = dataList.size();
                        invalidate();
                }

        }

        @Override
        protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
                super.onMeasure(widthMeasureSpec, heightMeasureSpec);
                width = getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec);
                //根据数据数量来得到控件高
                if (dataNum != 0) {
                        height = (dataNum + 1) * tableItemHeight;
                }
                //表格单元宽
                tableItemWidth = width / 13;
                setMeasuredDimension(width, height);
        }

        private int tableItemWidth;

        private int tableItemHeight = dip2px(getContext(), 36);//表格单元高

        /**
         * 表格左上角的横纵坐标
         */
        private float startX;

        private float startY;

        @Override
        protected void onDraw(Canvas canvas) {
                super.onDraw(canvas);

                //绘制统计表表第一列
                canvas.drawRect(startX, startY, startX + tableItemWidth, startY + tableItemHeight, mPaintLightGrey);
//        canvas.drawRect(startX + tableItemWidth, startY, width, startY + tableItemHeight, mPaintGreyBg);
                canvas.drawRect(startX, startY, width, startY + tableItemHeight, mPaintGreyBg);
                canvas.drawText("姓名", startX + tableItemWidth / 2, startY + tableItemHeight / 2 + textSize / 2, mPaintText);
                canvas.drawText("日期", startX + tableItemWidth * 3 / 2, startY + tableItemHeight / 2 + textSize / 2, mPaintText);
                canvas.drawText("对应时间段", startX + tableItemWidth * 5 / 2, startY + tableItemHeight / 2 + textSize / 2, mPaintText);
                canvas.drawText("上班时间", startX + tableItemWidth * 7 / 2, startY + tableItemHeight / 2 + textSize / 2, mPaintText);
                canvas.drawText("下班时间", startX + tableItemWidth * 9 / 2, startY + tableItemHeight / 2 + textSize / 2, mPaintText);
                canvas.drawText("签到时间", startX + tableItemWidth * 11 / 2, startY + tableItemHeight / 2 + textSize / 2, mPaintText);
                canvas.drawText("签退时间", startX + tableItemWidth * 13 / 2, startY + tableItemHeight / 2 + textSize / 2, mPaintText);
                canvas.drawText("应到", startX + tableItemWidth * 15 / 2, startY + tableItemHeight / 2 + textSize / 2, mPaintText);
                canvas.drawText("实到", startX + tableItemWidth * 17 / 2, startY + tableItemHeight / 2 + textSize / 2, mPaintText);
                canvas.drawText("迟到时间", startX + tableItemWidth * 19 / 2, startY + tableItemHeight / 2 + textSize / 2, mPaintText);
                canvas.drawText("早退时间", startX + tableItemWidth * 21 / 2, startY + tableItemHeight / 2 + textSize / 2, mPaintText);
                canvas.drawText("是否旷工", startX + tableItemWidth * 23 / 2, startY + tableItemHeight / 2 + textSize / 2, mPaintText);
                canvas.drawText("加班时间", startX + tableItemWidth * 25 / 2, startY + tableItemHeight / 2 + textSize / 2, mPaintText);
                canvas.drawLine(0, tableItemHeight, width, tableItemHeight, mPaintLine);


                for (int k = 1; k < dataNum + 2; k++) {
                        for (int m = 0; m < 12; m++) {
                                canvas.drawLine(tableItemWidth * (m + 1), tableItemHeight, tableItemWidth * (m + 1), tableItemHeight * k, mPaintLine);  //列
                        }
//                canvas.drawLine(tableItemWidth ,tableItemHeight , tableItemWidth, tableItemHeight *k, mPaintLine);  //列
                        for (int j = 1; j < 13; j++) {
                                canvas.drawLine(tableItemWidth * j, 0, tableItemWidth * j, tableItemHeight, mPaintLine);  // 行
                        }
                }

                for (int i = 0; i < dataNum; i++) {
                        startY += tableItemHeight;
                        canvas.drawRect(startX, startY, startX + tableItemWidth, startY + tableItemHeight, mPaintLightGrey);
//            if (i % 2 == 1) {
//                canvas.drawRect(startX + tableItemWidth, startY, width, startY + tableItemHeight, mPaintGreyBg);
//            } else {
//                canvas.drawRect(startX + tableItemWidth, startY, width, startY + tableItemHeight, mPaintWhiteBg);
//            }
                        canvas.drawText(dataList.get(i).getName(), startX + tableItemWidth / 2, startY + tableItemHeight / 2 + textSize / 2, mPaintText);
                        canvas.drawText(dataList.get(i).getDate() + "", startX + tableItemWidth * 3 / 2, startY + tableItemHeight / 2 + textSize / 2, mPaintText);
                        canvas.drawText(dataList.get(i).getCorrespondingTimePeriod() + "", startX + tableItemWidth * 5 / 2, startY + tableItemHeight / 2 + textSize / 2, mPaintText);
                        canvas.drawText(dataList.get(i).getToWorkTime() + "", startX + tableItemWidth * 7 / 2, startY + tableItemHeight / 2 + textSize / 2, mPaintText);
                        canvas.drawText(dataList.get(i).getOffWorkTime() + "", startX + tableItemWidth * 9 / 2, startY + tableItemHeight / 2 + textSize / 2, mPaintText);
                        canvas.drawText(dataList.get(i).getCheckInTime() + "", startX + tableItemWidth * 11 / 2, startY + tableItemHeight / 2 + textSize / 2, mPaintText);
                        canvas.drawText(dataList.get(i).getCheckoutTime() + "", startX + tableItemWidth * 13 / 2, startY + tableItemHeight / 2 + textSize / 2, mPaintText);
                        canvas.drawText(dataList.get(i).getShouldArriveTime() + "", startX + tableItemWidth * 15 / 2, startY + tableItemHeight / 2 + textSize / 2, mPaintText);
                        canvas.drawText(dataList.get(i).getReallyTime() + "", startX + tableItemWidth * 17 / 2, startY + tableItemHeight / 2 + textSize / 2, mPaintText);
                        canvas.drawText(dataList.get(i).getLateTime() + "", startX + tableItemWidth * 19 / 2, startY + tableItemHeight / 2 + textSize / 2, mPaintText);
                        canvas.drawText(dataList.get(i).getLeaveEarly() + "", startX + tableItemWidth * 21 / 2, startY + tableItemHeight / 2 + textSize / 2, mPaintText);
                        canvas.drawText(dataList.get(i).getIsAbsenteeism() + "", startX + tableItemWidth * 23 / 2, startY + tableItemHeight / 2 + textSize / 2, mPaintText);
                        canvas.drawText(dataList.get(i).getOverTime() + "", startX + tableItemWidth * 25 / 2, startY + tableItemHeight / 2 + textSize / 2, mPaintText);

                        canvas.drawLine(0, tableItemHeight * (i + 1), width, tableItemHeight * (i + 1), mPaintLine);


//            for(int k = 1;k< dataNum +1 ;k++){
//                for (int m=0;m<12;m++){
//                    canvas.drawLine(tableItemWidth * (m+1) ,tableItemHeight , tableItemWidth * (m+1), tableItemHeight *k, mPaintLine);  //列
//                }
//                canvas.drawLine(tableItemWidth ,tableItemHeight , tableItemWidth, tableItemHeight *k, mPaintLine);  //列
                        for (int j = 1; j <= 13; j++) {
                                if (j != 13) {
                                        canvas.drawLine(tableItemWidth * j, 0, tableItemWidth * j, tableItemHeight, mPaintLine);  // 行
                                }
                                canvas.drawLine(tableItemWidth * 0, tableItemHeight * (dataNum + 1), tableItemWidth * j, tableItemHeight * (dataNum + 1), mPaintLine);
                        }

//            }

//            startY += tableItemHeight;
                }

                startX = 0;
                startY = 0;
        }

        /**
         * 将dp转换为sp
         */
        public int dip2px(Context context, float dipValue) {
                final float scale = context.getResources().getDisplayMetrics().density;
                return (int) (dipValue * scale + 0.5f);
        }

        /**
         * sp转dp
         */
        public int px2dip(Context context, float pxValue) {
                final float scale = context.getResources().getDisplayMetrics().density;
                return (int) (pxValue / scale + 0.5f);
        }
}
